//
//  myCollectionViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/8/7.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "myCollectionViewController.h"
#import "bottomTableViewCell.h"
#import "HomeCrowdfundingViewController.h"
#import "MJRefresh.h"

@interface myCollectionViewController (){

    int mjMark;
    int page;
    
}

@end

@implementation myCollectionViewController

- (void)viewWillAppear:(BOOL)animated{
    
    //下拉刷新
    _pianoListTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        mjMark = 1;
        [self requestData];
        [_pianoListTableView.mj_header endRefreshing];
        
    }];
    
    //上拉加载
    _pianoListTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page ++;
        mjMark = 2;
        [self requestData];
        [_pianoListTableView.mj_footer endRefreshing];
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self requestData];
}

#pragma mark ----------------------- requestData ---------------------

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setValue: [NSString stringWithFormat:@"%d",page] forKey:@"page"];
    
    [[MNetworkHandler sharedInstance]conURL:more_collection
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       if (mjMark == 1) {
                                           
                                           _dataArray = returnData[@"data"];
                                           
                                       }else{
                                           
                                           [_dataArray addObjectsFromArray:returnData[@"data"]];
                                           
                                       }
                                       _dataArray = returnData[@"data"];
                                       NSLog(@"\n\n\n===我的收藏===\n\n\n%@",_dataArray);
                                       [_pianoListTableView reloadData];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

#pragma mark ------------------ init -------------------

- (void)initView{
    
    self.title = @"我的收藏";
    
    mjMark = 1;
    page = 1;
    
    _pianoListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), scrHeight() - 64)];
    _pianoListTableView.backgroundColor = getColor(@"f3f5f7");
    _pianoListTableView.delegate = self;
    _pianoListTableView.dataSource = self;
    _pianoListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_pianoListTableView];
    
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(1), scrWidth(), screenScale(0.7))];
    threadView.backgroundColor = getColor(@"cccccc");
    [self.view addSubview:threadView];
}

#pragma mark ------------------ click -------------------

- (void)searchBtnClick{
    
    NSLog(@"搜索");
    
    //    searchViewController *searchVC = [[searchViewController alloc]init];
    //    [self.navigationController pushViewController:searchVC animated:YES];
}

#pragma mark -------------------- tableView ----------------------

#pragma mark --- UITableViewDelegate ---

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return screenScale(190);
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeCrowdfundingViewController *vc = [[HomeCrowdfundingViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark --- UITableViewDataSource ---

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView registerClass:[bottomTableViewCell class] forCellReuseIdentifier:@"bottomTableViewCell"];
    bottomTableViewCell *cell = (bottomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"bottomTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSString *urlN = manage(AP_HOST, _dataArray[indexPath.row][@"pic"]);
    NSURL *urlW = [NSURL URLWithString:urlN];
    [cell.leftImg sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
    
    cell.nameLable.text = _dataArray[indexPath.row][@"title"];
    
    cell.textLable.text = _dataArray[indexPath.row][@"content"];
    
    [cell.progressView setProgress:[_dataArray[indexPath.row][@"money"] floatValue] / [_dataArray[indexPath.row][@"target"] floatValue] animated:YES];
    
    //        [cell.progressView setProgress:30.0 / 100.0 animated:YES];
    
    cell.progressLable.text = [[NSString stringWithFormat:@"%.0f",[_dataArray[indexPath.row][@"money"] floatValue] / [_dataArray[indexPath.row][@"target"] floatValue]*100] stringByAppendingString:@"%"];
    
    NSString *urlN1 = manage(AP_HOST, _dataArray[indexPath.row][@"header_img"]);
    NSURL *urlW1 = [NSURL URLWithString:urlN1];
    [cell.headImg sd_setImageWithURL:urlW1 placeholderImage:[UIImage imageNamed:@"niunai"]];
    
    cell.personLable.text = _dataArray[indexPath.row][@"user_name"];
    
    cell.moneyLable.text = _dataArray[indexPath.row][@"target"];
    
    cell.peopleLable.text = _dataArray[indexPath.row][@"support"];
    
    cell.dayLable.text = _dataArray[indexPath.row][@"days"];
    
    return cell;
}

@end
